<template>
  <base-breadcrumb title="科技成果完成">
    <div v-loading="loading" class="about-master-container">
      <a-card>
        <title-name title="基础信息" />
        <a-form-model>
          <a-row>
            <a-col span="8">
              <a-form-model-item label="所属分公司">
                <BaseInput disabled v-model="form.branchCompany" placeholder="请输入" />
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item label="项目名称">
                <BaseInput disabled v-model="form.projectName" placeholder="请输入" />
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </a-card>
      <a-card v-if="Plan">
        <title-name title="科技管理计划信息" />
        <a-form-model>
          <a-row>
            <a-col span="8">
              <a-form-model-item label="成果名称">
                <BaseInput disabled v-model="Plan.planAchievementsName" placeholder="请输入" />
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item label="成果分类">
                <BaseInput disabled v-model="Plan.achievementsType" placeholder="请输入" />
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item label="成果等级">
                <BaseInput disabled v-model="Plan.planAchievementsLevel" placeholder="请输入" />
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item label="计划完成人">
                <a-tag>{{ Plan.planCompleteEmployee }}</a-tag>
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col span="8">
              <a-form-model-item label="计划完成时间">
                <BaseInput disabled v-model="Plan.planCompleteDate" placeholder="请输入" />
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item label="计划附件">
                <file-link :file-list="Plan.fileArrays" />
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </a-card>
      <a-card v-if="declare">
        <title-name title="科技成果申报信息" />
        <a-form-model>
          <a-row>
            <a-col span="8">
              <a-form-model-item label="成果名称">
                <BaseInput disabled v-model="declare.declareAchievementsName" placeholder="请输入" />
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item label="成果分类">
                <BaseInput disabled v-model="declare.declareType" placeholder="请输入" />
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item label="成果等级">
                <BaseInput disabled v-model="declare.declareAchievementsLevel" placeholder="请输入" />
              </a-form-model-item>
            </a-col>
            <a-col span="24">
              <a-form-model-item label="申报人">
                <a-tag v-for="item in declare.joinEmployee" :key="item.employeeName">
                  {{ item.employeeName }}
                </a-tag>
                <span v-if="!declare.joinEmployee || declare.joinEmployee.length == 0">无</span>
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item label="申报日期">
                <BaseInput disabled v-model="declare.declareDate" placeholder="请输入" />
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item label="附件">
                <file-link :file-list="declare.fileArrays" />
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </a-card>
      <a-card>
        <title-name title="科技成果完成情况" />
        <a-form-model ref="form" :model="queryParam" :rules="rules">
          <a-row>
            <a-col span="8">
              <a-form-model-item label="完成状态" prop="completionStatus">
                <a-select v-model="queryParam.completionStatus" placeholder="请选择">
                  <a-select-option value="4">
                    获得
                  </a-select-option>
                  <a-select-option value="5">
                    未获得
                  </a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <template v-if="queryParam.completionStatus === '4'">
              <a-col span="8">
                <a-form-model-item label="完成成果名称" prop="completeAchievementsName">
                  <BaseInput v-model.trim="queryParam.completeAchievementsName" placeholder="请输入完成成果名称" />
                </a-form-model-item>
              </a-col>
              <a-col span="8">
                <a-form-model-item label="成果分类" prop="achievementsTypeId">
                  <a-select v-model="queryParam.achievementsTypeId" placeholder="请选择" @change="getLevelList($event)">
                    <a-select-option v-for="item in ClassifyList" :key="item.id" :value="item.id">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col span="8" v-if="queryParam.achievementsTypeId == '683867596952571904'">
                <a-form-model-item label="期刊类型" prop="periodicalTypeId">
                  <a-select v-model="queryParam.periodicalTypeId" placeholder="请选择">
                    <a-select-option v-for="item in typeList" :key="item.id" :value="item.id">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col span="8">
                <a-form-model-item label="成果等级" prop="completeAchievementsLevelId">
                  <a-select v-model="queryParam.completeAchievementsLevelId" placeholder="请选择" @change="completeAchievementsLevelIdChange($event)">
                    <a-select-option v-for="item in LevelList" :key="item.id" :value="item.id">
                      {{ item.levelName }}
                    </a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col span="16">
                <a-form-model-item label="完成人" prop="completeEmployee">
                  <div>
                    <a-button v-if="queryParam.completeEmployee.length < canAddPeopleLength" style="margin-right: 10px" :disabled="!completeAchievementsLevelName1" @click="visible2 = true">
                      添加内部人员
                    </a-button>
                    <a-button v-if="queryParam.completeEmployee.length < canAddPeopleLength" style="margin-right: 10px" :disabled="!completeAchievementsLevelName1" @click="visible3 = true">
                      添加外部人员
                    </a-button>
                    <a-button style="margin-right: 10px" v-show="queryParam.completeEmployee.length > 0" @click="sortDataChange(-1)">
                      前移
                    </a-button>
                    <a-button v-show="queryParam.completeEmployee.length > 0" style="margin-right: 10px" @click="sortDataChange(1)">
                      后移
                    </a-button>
                  </div>
                  <!-- @close="queryParam.completeEmployee = queryParam.completeEmployee.filter((v) => v.id !== item.id)" -->
                  <a-tag v-for="(item, index) in queryParam.completeEmployee" :key="item.id" closable :class="sortDataIndex === index ? 'active' : ''" @close="delTagFn(item.id)">
                    <span class="tagName" @click="sortDataSelect(item, index)">{{ item.name }}</span>
                  </a-tag>
                  <span class="del_btn mr20">（按证书上面完成人顺序填写）</span>
                </a-form-model-item>
              </a-col>
            </template>
          </a-row>
          <template v-if="queryParam.completionStatus === '4'">
            <a-row>
              <a-col span="8">
                <a-form-model-item label="完成日期" prop="completeDate">
                  <a-date-picker :disabledDate="disabledDate" v-model="queryParam.completeDate" type="date" value-format="YYYY-MM-DD HH:mm:ss" />
                </a-form-model-item>
              </a-col>
              <a-col span="8">
                <a-form-model-item label="标引" prop="indexInfo">
                  <a-tree-select
                    multiple
                    :treeDefaultExpandedKeys="treeDefaultExpandedKeys"
                    :value="queryParam.indexInfo"
                    style="width: 100%"
                    :tree-data="indexInfoList"
                    :replaceFields="{ children: 'children', title: 'name', key: 'id', value: 'id' }"
                    :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                    placeholder="请选择"
                    @change="onChange"
                  ></a-tree-select>
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row>
              <template v-if="achievementsTypeName === '科技奖'">
                <a-col span="8">
                  <a-form-model-item prop="awardName" label="授奖称号">
                    <BaseInput v-model.trim="queryParam.awardName" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="awardDpt" label="授奖单位">
                    <BaseInput v-model.trim="queryParam.awardDpt" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="证书编号" prop="certificateCode" :key="1">
                    <BaseInput v-model.trim="queryParam.certificateCode" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
              </template>

              <!--                研发立项-->
              <template v-if="achievementsTypeName === '研发立项'">
                <a-col span="8">
                  <a-form-model-item prop="projectCode" label="立项编号">
                    <BaseInput v-model.trim="queryParam.projectCode" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="实施情况" :key="3">
                    <BaseInput v-model="queryParam.progressSituation" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="beginAndEndTime" label="起始时间">
                    <a-date-picker v-model="queryParam.beginAndEndTime" type="date" value-format="YYYY-MM-DD HH:mm:ss" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="planDate" label="计划验收时间">
                    <a-date-picker v-model="queryParam.planDate" type="date" value-format="YYYY-MM-DD HH:mm:ss" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="实际验收时间" :key="4">
                    <a-date-picker v-model="queryParam.factDate" type="date" value-format="YYYY-MM-DD HH:mm:ss" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="totalFee" label="总经费（万）">
                    <BaseInputNumber :precision="6" v-model="queryParam.totalFee" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="subsidizeFee" label="资助经费（万）">
                    <BaseInputNumber :precision="6" v-model.trim="queryParam.subsidizeFee" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="classPrincipalPeople" label="课题负责人">
                    <BaseInput v-model.trim="queryParam.classPrincipalPeople" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="证书编号" :key="5">
                    <BaseInput v-model.trim="queryParam.certificateCode" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="课题研发人员" :key="6">
                    <BaseInput v-model.trim="queryParam.classDiscoveryPeople" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
              </template>

              <!--                示范工程立项-->
              <template v-if="achievementsTypeName === '示范工程立项'">
                <a-col span="8">
                  <a-form-model-item prop="projectCode" label="立项编号">
                    <BaseInput v-model.trim="queryParam.projectCode" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="实施情况" :key="8">
                    <BaseInput v-model="queryParam.progressSituation" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="实施状态" :key="9">
                    <BaseInput v-model.trim="queryParam.implementStatus" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="planDate" label="计划验收时间">
                    <a-date-picker v-model="queryParam.planDate" type="date" value-format="YYYY-MM-DD HH:mm:ss" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="实际验收时间" :key="11">
                    <a-date-picker v-model="queryParam.factDate" type="date" value-format="YYYY-MM-DD HH:mm:ss" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="projectOrg" label="立项机构">
                    <BaseInput v-model.trim="queryParam.projectOrg" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="projectLevel" label="立项级别">
                    <BaseInputNumber v-model.trim="queryParam.projectLevel" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="projectPrincipalPeople" label="项目负责人">
                    <BaseInput v-model.trim="queryParam.projectPrincipalPeople" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="主要推广人员" :key="12">
                    <BaseInput v-model.trim="queryParam.majorPromotePeople" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="placeInfo" label="地点">
                    <BaseInput v-model.trim="queryParam.placeInfo" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="startAndEndTime" label="计划开竣工时间">
                    <a-range-picker v-model="queryParam.startAndEndTime" type="date" value-format="YYYY-MM-DD HH:mm:ss" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="scope" label="规模">
                    <BaseInput v-model.trim="queryParam.scope" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="证书编号" :key="13">
                    <BaseInput v-model.trim="queryParam.certificateCode" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
              </template>

              <!--                工法-->
              <template v-if="achievementsTypeName === '工法'">
                <a-col span="8">
                  <a-form-model-item prop="constructionCodeNo" label="工法编号">
                    <BaseInput v-model.trim="queryParam.constructionCodeNo" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="year" label="年度">
                    <BaseInputNumber v-model.trim="queryParam.year" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="awardDpt" label="授予单位">
                    <BaseInput v-model.trim="queryParam.awardDpt" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
              </template>

              <!--                国家知识产权-专利-->
              <template v-if="achievementsTypeName === '国家知识产权' && (completeAchievementsLevelName1 === '发明专利' || completeAchievementsLevelName1 === '实用新型专利')">
                <a-col span="8">
                  <a-form-model-item prop="patentNo" label="专利号">
                    <BaseInput v-model.trim="queryParam.patentNo" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="applyDay" label="申请日">
                    <a-date-picker v-model="queryParam.applyDay" type="date" value-format="YYYY-MM-DD HH:mm:ss" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="公告日">
                    <a-date-picker v-model="queryParam.announcementDay" type="date" value-format="YYYY-MM-DD HH:mm:ss" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="zone" label="片区">
                    <BaseInput v-model.trim="queryParam.zone" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="状态">
                    <a-select v-model="queryParam.teachStatus">
                      <a-select-option value="1">授权</a-select-option>
                      <a-select-option value="2">受理</a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
              </template>

              <!--                国家知识产权-软著-->
              <template v-if="achievementsTypeName === '国家知识产权' && completeAchievementsLevelName1 === '计算机软件著作权'">
                <a-col span="8">
                  <a-form-model-item prop="registerNo" label="登记号">
                    <BaseInput v-model.trim="queryParam.registerNo" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="developmentCompletedDay" label="开发完成日">
                    <a-date-picker v-model="queryParam.developmentCompletedDay" type="date" value-format="YYYY-MM-DD HH:mm:ss" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="publicationDay" label="首次发表日">
                    <a-date-picker v-model="queryParam.publicationDay" type="date" value-format="YYYY-MM-DD HH:mm:ss" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="wayOfPower" label="权利取得方式">
                    <BaseInput v-model.trim="queryParam.wayOfPower" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="zone" label="片区">
                    <BaseInput v-model.trim="queryParam.zone" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="状态" style="opacity: 0;">
                    <BaseInput v-model="queryParam.zone" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
              </template>

              <!--                论文-->
              <template v-if="achievementsTypeName === '论文'">
                <a-col span="8">
                  <a-form-model-item prop="journal" label="刊物">
                    <BaseInput v-model.trim="queryParam.journal" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="year" label="年度">
                    <BaseInputNumber v-model.trim="queryParam.year" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="periodsNum" label="期数">
                    <BaseInputNumber v-model.trim="queryParam.periodsNum" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="paperSituation" label="论文情况">
                    <BaseInput v-model.trim="queryParam.paperSituation" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="leadAuthorJobTitle" label="第一作者职称">
                    <BaseInput v-model.trim="queryParam.leadAuthorJobTitle" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="leadAuthorPosition" label="第一作者职务">
                    <BaseInput v-model.trim="queryParam.leadAuthorPosition" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="leadAuthorSpecialty" label="第一作者专业">
                    <BaseInput v-model.trim="queryParam.leadAuthorSpecialty" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
              </template>

              <!--            科技成果评价-->
              <template v-if="achievementsTypeName === '科技成果评价'">
                <a-col span="8">
                  <a-form-model-item prop="appraiseDate" label="鉴定（验收）年月">
                    <a-date-picker v-model="queryParam.appraiseDate" type="date" value-format="YYYY-MM-DD HH:mm:ss" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="presideAgency" label="主持机构">
                    <BaseInput v-model="queryParam.presideAgency" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="24">
                  <a-form-model-item prop="appraiseSummary" label="鉴定（验收）结论摘要">
                    <BaseInput v-model="queryParam.appraiseSummary" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="appraiseMember" label="鉴定（验收）主要成员">
                    <BaseInput v-model="queryParam.appraiseMember" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
              </template>

              <!--            科技查新-->
              <template v-if="achievementsTypeName === '科技查新'">
                <a-col span="8">
                  <a-form-model-item prop="commissionDate" label="委托时间">
                    <a-date-picker v-model="queryParam.commissionDate" type="date" value-format="YYYY-MM-DD HH:mm:ss" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="reportCode" label="报告编号">
                    <BaseInput v-model="queryParam.reportCode" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item prop="newAgency" label="查新机构">
                    <BaseInput v-model="queryParam.newAgency" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
                <a-col span="24">
                  <a-form-model-item prop="findPurpose" label="查新目的">
                    <BaseInput v-model="queryParam.findPurpose" placeholder="请输入" />
                  </a-form-model-item>
                </a-col>
              </template>

              <a-col span="16">
                <a-row>
                  <a-col span="12">
                    <a-form-model-item prop="completeOrgName" label="成果完成单位">
                      <a-select show-search @change="completeOrgChange" :filter-option="filterOption" mode="multiple" :maxTagCount="1" v-model="queryParam.completeData" placeholder="请选择">
                        <a-select-option :key="item.id" :value="item.id" v-for="item in completeOrgList">{{ item.name }}</a-select-option>
                      </a-select>

                      <a-tag v-for="(sub, index) in completeListData" :key="sub" closable :class="completeIndex === index ? 'active' : ''" @close="completeClose(sub, index)">
                        <span class="tagName" @click="completeSelect(sub, index)">{{ sub }}</span>
                      </a-tag>
                    </a-form-model-item>
                  </a-col>
                  <a-col span="12">
                    <a-button style="margin-left:10px;margin-top:4px" @click="completeAddFc">添加</a-button>
                    <a-button style="margin-left:10px;margin-top:4px" v-show="completeListData.length > 0" @click="completeListChange(-1)">前移</a-button>
                    <a-button style="margin-left:10px;margin-top:4px" v-show="completeListData.length > 0" @click="completeListChange(1)">后移</a-button></a-col
                  >
                </a-row>
              </a-col>
              <a-col span="16">
                <a-form-model-item label="备注">
                  <BaseInput v-model.trim="queryParam.remark" placeholder="请输入" />
                </a-form-model-item>
              </a-col>
              <a-col span="24">
                <a-form-model-item label="附件上传" prop="fileArrays" :auto-link="false" ref="fileArrays">
                  <upload-list :is-limit="true" business-code="STM05" :upload-arrys.sync="queryParam.fileArrays" />
                </a-form-model-item>
              </a-col>
            </a-row>
          </template>
        </a-form-model>
      </a-card>
      <div class="pull-right page-btn-right-top">
        <a-button style="margin: 0 10px" type="primary" @click="submitComplete">提交</a-button>
        <a-button style="margin: 0 10px" @click="$close()">关闭</a-button>
      </div>
    </div>
    <SelectPerson :max-select-length="canAddPeopleLength" :visible.sync="visible2" title="添加内部人员" :list.sync="queryParam.completeEmployeeInside" @getSelectPerson="getSelectPersonInside" />
    <SelectPersonExt :max-select-length="canAddPeopleLength" :visible.sync="visible3" title="添加外部人员" :list.sync="queryParam.completeEmployeeExternal" @getSelectPerson="getSelectPersonExternal" />
    <selectApprover :businessId="businessId" :processConfig="setProcessConfig"></selectApprover>
  </base-breadcrumb>
</template>

<script>
import { getDeclare, addComplete, titleLabelQuery } from '@/api/achievementsManage'
import { getClassifyList, getGradeList, completeAdd, completeList } from '@/api/achievementsType'
import { typesLabelList } from '@/api/science'
import Madel from '../plots/components/Madel'
import deleteFun from './delete.js'
import Model from '@/views/science/plots/components/Model'
import BaseEditPage from '@/views/baseEditPage'
import UploadList from '@/components/UploadList'
import SelectPerson from './components/personSelect'
import SelectPersonExt from './components/SelectPerson'
import moment from 'moment'

export default {
  extends: new BaseEditPage(),
  data() {
    return {
      typeList: [],
      sortDataIndex: null,
      sortData: null,
      completeIndex: null,
      completeData: null,
      queryParam: {
        beginAndEndTime: '',
        completionStatus: undefined,
        completeEmployee: [],
        completeOrgName: [],
        completeEmployeeInside: [],
        completeEmployeeExternal: [],
        fileArrays: [],
        indexInfo: [],
        completeOrgDTOS: [],
        completeData: []
      },
      completeOrgList: [
        { name: '中国建筑第八工程局有限公司', id: '1' },
        { name: '中国建筑第八工程局有限公司西南分公司', id: '2' },
        { name: '中建八局西南建设工程有限公司', id: '3' },
        { name: '中建八局两江建设工程有限公司', id: '4' },
        { name: '中建八局中南建设工程有限公司', id: '5' }
      ],
      Plan: null,
      declare: null,
      rules: {
        appraiseDate: [{ required: true, message: '请选择鉴定（验收）年月', trigger: 'change' }],
        commissionDate: [{ required: true, message: '请选择委托时间', trigger: 'change' }],
        presideAgency: [{ required: true, message: '请输入主持机构', trigger: 'change' }],
        newAgency: [{ required: true, message: '请输入查新机构', trigger: 'change' }],
        appraiseSummary: [{ required: true, message: '请输入鉴定（验收）结论摘要', trigger: 'change' }],
        appraiseMember: [{ required: true, message: '请输入鉴定（验收）主要成员', trigger: 'change' }],
        reportCode: [{ required: true, message: '请输入报告编号', trigger: 'change' }],
        findPurpose: [{ required: true, message: '请输入查新目的', trigger: 'change' }],

        completionStatus: [{ required: true, message: '请选择完成状态', trigger: 'change' }],
        completeAchievementsName: [{ required: true, message: '请输入完成成果名称', trigger: 'change' }],
        achievementsTypeId: [{ required: true, message: '请选择成果分类', trigger: 'change' }],
        periodicalTypeId: [{ required: true, message: '请选择期刊类型', trigger: 'change' }],
        completeAchievementsLevelId: [{ required: true, message: '请选择完成成果等级', trigger: 'change' }],
        joinEmployee: [{ type: 'array', required: true, message: '请选择参与人', trigger: 'change' }],
        completeDate: [{ required: true, message: '请选择完成日期', trigger: 'change' }],
        completeEmployee: [{ type: 'array', required: true, message: '请选择完成人', trigger: 'change' }],
        fileArrays: [{ type: 'array', required: true, message: '请上传附件', trigger: 'change' }],
        awardingUnit: [{ required: true, message: '请输入授奖单位', trigger: 'change' }],
        achievementsNo: [{ required: true, message: '请输入成果编号', trigger: 'change' }],

        announcementDay: [{ required: true, message: '请输入公告日', trigger: 'change' }],
        applyDay: [{ required: true, message: '请输入申请日', trigger: 'change' }],
        awardDpt: [{ required: true, message: '请输入授奖单位', trigger: 'change' }],
        awardName: [{ required: true, message: '请输入授奖称号', trigger: 'change' }],
        beginAndEndTime: [{ required: true, message: '请输入起始时间', trigger: 'change' }],
        certificateCode: [{ required: true, message: '请输入证书编号', trigger: 'change' }],
        classDiscoveryPeople: [{ required: true, message: '请输入课题研发人员', trigger: 'change' }],
        classPrincipalPeople: [{ required: true, message: '请输入课题负责人', trigger: 'change' }],
        completeOrgId: [{ required: true, message: '请输入成果完成单位ID', trigger: 'change' }],
        completeOrgName: [{ required: true, message: '请输入成果完成单位名称', trigger: 'change' }],
        constructionCodeNo: [{ required: true, message: '请输入工法编号', trigger: 'change' }],
        developmentCompletedDay: [{ required: true, message: '请输入开发完成日', trigger: 'change' }],
        factDate: [{ required: true, message: '请输入实际验收时间', trigger: 'change' }],
        implementStatus: [{ required: true, message: '请输入实施状态', trigger: 'change' }],
        indexInfo: [{ required: true, message: '请选择标引', trigger: 'change' }],
        journal: [{ required: true, message: '请输入刊物', trigger: 'change' }],
        leadAuthorJobTitle: [{ required: true, message: '请输入第一作者职称', trigger: 'change' }],
        leadAuthorPosition: [{ required: true, message: '请输入第一作者职务', trigger: 'change' }],
        leadAuthorSpecialty: [{ required: true, message: '请输入第一作者专业', trigger: 'change' }],
        majorPromotePeople: [{ required: true, message: '请输入主要推广人员', trigger: 'change' }],
        paperSituation: [{ required: true, message: '请输入论文情况', trigger: 'change' }],
        patentNo: [{ required: true, message: '请输入专利号', trigger: 'change' }],
        periodsNum: [{ required: true, message: '请输入期数', trigger: 'change' }],
        planDate: [{ required: true, message: '请输入计划验收时间', trigger: 'change' }],
        progressSituation: [{ required: true, message: '请输入实施情况', trigger: 'change' }],
        projectCode: [{ required: true, message: '请输入立项编号', trigger: 'change' }],
        projectLevel: [{ required: true, message: '请输入立项级别', trigger: 'change' }],
        projectOrg: [{ required: true, message: '请输入立项机构', trigger: 'change' }],
        projectPrincipalPeople: [{ required: true, message: '请输入项目负责人', trigger: 'change' }],
        publicationDay: [{ required: true, message: '请输入首次发表日', trigger: 'change' }],
        registerDay: [{ required: true, message: '请输入登记日', trigger: 'change' }],
        scope: [{ required: true, message: '请输入规模', trigger: 'change' }],
        startAndEndTime: [{ required: true, message: '请输入计划开竣工时间', trigger: 'change' }],
        subsidizeFee: [{ required: true, message: '请输入资助经费', trigger: 'change' }],
        teachStatus: [{ required: true, message: '请输入状态', trigger: 'change' }],
        totalFee: [{ required: true, message: '请输入总经费', trigger: 'change' }],
        wayOfPower: [{ required: true, message: '请输入权力获取方式', trigger: 'change' }],
        year: [{ required: true, message: '请输入年度', trigger: 'change' }],
        zone: [{ required: true, message: '请输入片区', trigger: 'change' }],
        placeInfo: [{ required: true, message: '请输入地点', trigger: 'change' }],
        registerNo: [{ required: true, message: '请输入登记号', trigger: 'change' }]
      },
      form: {},
      addCompleteAchievements: { flag: 'true', text: '' },
      loading: false,
      ClassifyList: [],
      LevelList: [],
      indexInfoList: [],
      completeListData: [],
      treeDefaultExpandedKeys: [],
      treeSelectKeys: [],
      disabled: false,
      visible2: false,
      visible3: false,
      init: false,
      achievementsTypeName: '',
      completeAchievementsLevelName1: '',
      //获取流程配置值
      processConfig: null,
      //点击提交发送流程配置值
      setProcessConfig: null,
      businessId: null
    }
  },
  components: {
    UploadList,
    SelectPerson,
    SelectPersonExt
  },
  computed: {
    canAddPeopleLength() {
      if (this.achievementsTypeName === '研发立项' || this.achievementsTypeName === '示范工程立项') {
        if (this.completeAchievementsLevelName1.includes('局') || this.completeAchievementsLevelName1.includes('省')) {
          return 15
        }
      }
      return 100
    }
  },
  watch: {
    'queryParam.fileArrays'(newVal, oldVal) {
      if (this.init) {
        this.$refs.fileArrays.onFieldChange()
      }
    },
    'queryParam.completeEmployee'(newVal, oldVal) {
      if (this.init && newVal !== oldVal) {
        this.$refs.form.validateField('completeEmployee')
      }
    }
  },
  async mounted() {
    await this.getIndexInfoList()
    await this.getData()
    //获取通过获取流程配置值
    this.processConfig = await this.$getConfigFn('achievements_complete')
    this.completeListFc()
    this.typesLabelListFn()
  },
  methods: {
    async typesLabelListFn() {
      let res = await typesLabelList({ type: 6 })
      this.typeList = res.data || []
    },
    async completeAddFc() {
      if (!this.addCompleteAchievements.text){
        this.$message.warning('请输入需要添加的数据');
        return false
      }
      // this.addCompleteAchievements.text
     let obj =  this.completeOrgList.find(item=> item.name === this.addCompleteAchievements.text)
      if(obj) {
        this.$message.warning('该数据已存在');
        return
      }
      this.addCompleteAchievements.flag = true
      let param = { enabled: true, name: this.addCompleteAchievements.text, type: 'COMPLETE_ORG' }
      let res = await completeAdd(param)
      if (res.code === 200) {
        this.$message.success('成功')
         this.addCompleteAchievements = {}
        this.completeListFc()
      } else {
        this.$message.error()
        ;('失败')
      }
    },
    async completeListFc() {
      // this.addCompleteAchievements.flag = true
      let param = { type: '5' }
      let res = await completeList(param)
      if (res.code === 200 && res.data) {
        this.completeOrgList = res.data
        this.$forceUpdate()
      }
    },
    /**
     * @description: 过滤数据
     * @param
     * @return {res.data}
     */
    filterOption(input, option) {
      let arr = option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      this.addCompleteAchievements = { flag: arr, text: input }
      return arr
    },
    completeClose(item, index) {
      this.completeIndex = null
      let selectData = this.completeOrgList.filter(v => v.name === item)
      this.queryParam.completeOrgDTOS = this.queryParam.completeOrgDTOS.filter(v => v.completeOrgName !== item)
      this.completeListData = this.completeListData.filter(v => v !== item)
      this.queryParam.completeData = this.queryParam.completeData.filter(sub => sub !== selectData[0].id)
      this.queryParam.completeOrgDTOS = this.queryParam.completeOrgDTOS.map(sub=>{
        if (sub.sortNum > index){
          sub.sortNum  = sub.sortNum -1
        }
        return sub
      })
      this.$forceUpdate()
    },
    completeSelect(val, index) {
      this.completeIndex = index
      this.completeData = val
    },
    completeOrgChange(val) {
      this.queryParam.completeOrgDTOS = []
      this.queryParam.completeOrgName = []
      val.forEach((item, idnex) => {
        this.completeOrgList.forEach(sub => {
          if (item === sub.id) {
            this.queryParam.completeOrgName.push(sub.name)
            this.queryParam.completeOrgDTOS.push({
              completeOrgName: sub.name,
              sortNum: idnex,
              completeOrgId: sub.id
            })
          }
        })
      })
      this.completeListData = this.queryParam.completeOrgName
    },
    completeListChange(index) {
      if (index === -1 && this.completeIndex === 0 && this.completeData) {
        return false
      }
      if (!this.completeData && !this.completeIndex) {
        this.$message.warning('请选择需要移动的数据')
        return false
      }
      if (this.completeIndex + 1 == this.completeListData.length && index === 1) {
        return false
      }
      if (this.completeIndex === 0 && index === -1) {
        return false
      }
      let data = this.completeListData[this.completeIndex + index]
      this.completeListData[this.completeIndex + index] = this.completeData
      this.completeListData[this.completeIndex] = data

      let completeOrgDTOS = this.queryParam.completeOrgDTOS[this.completeIndex]
      let completeOrgDTOSIndex = this.queryParam.completeOrgDTOS[this.completeIndex+index]
      let completeOrgDTOSNum = this.$clone(this.queryParam.completeOrgDTOS[this.completeIndex].sortNum)
      let completeOrgDTOSIndexNum = this.$clone(this.queryParam.completeOrgDTOS[this.completeIndex+index].sortNum)

      this.queryParam.completeOrgDTOS[this.completeIndex].sortNum = completeOrgDTOSIndexNum
      this.queryParam.completeOrgDTOS[this.completeIndex+index].sortNum = completeOrgDTOSNum
      this.queryParam.completeOrgDTOS[this.completeIndex] = completeOrgDTOSIndex
      this.queryParam.completeOrgDTOS[this.completeIndex+index] = completeOrgDTOS

      this.completeIndex += index
      this.$forceUpdate()
    },
    delTagFn(id) {
      this.queryParam.completeEmployee = this.queryParam.completeEmployee.filter(v => v.id !== id)
      this.queryParam.completeEmployeeExternal = this.queryParam.completeEmployeeExternal.filter(v => v.id !== id)
      this.queryParam.completeEmployeeInside = this.queryParam.completeEmployeeInside.filter(v => v.id !== id)
    },
    sortDataSelect(val, index) {
      this.sortDataIndex = index
      this.sortData = val
    },
    sortDataChange(index) {
      if (index === -1 && this.sortDataIndex === 0 && this.sortData) {
        return false
      }
      if (!this.sortData && !this.sortDataIndex) {
        this.$message.warning('请选择需要移动的数据')
        return false
      }
      if (this.sortDataIndex + 1 == this.queryParam.completeEmployee.length && index === 1) {
        return false
      }
      if (this.sortDataIndex === 0 && index === -1) {
        return false
      }
      let data = this.queryParam.completeEmployee[this.sortDataIndex + index]
      this.queryParam.completeEmployee[this.sortDataIndex + index] = this.sortData
      this.queryParam.completeEmployee[this.sortDataIndex] = data
      this.sortDataIndex += index
      this.$forceUpdate()
    },
    getSelectPersonInside(val) {
      val = val.map(sub => {
        sub.type = 0
        return sub
      })
      this.queryParam.completeEmployeeInside = val
      if (this.queryParam.completeEmployeeExternal) {
        this.queryParam.completeEmployee = this.queryParam.completeEmployeeExternal.concat(val)
        this.$forceUpdate()
      } else {
        this.queryParam.completeEmployee = val
        this.$forceUpdate()
      }
    },
    getSelectPersonExternal(val) {
      val = val.map(sub => {
        sub.type = 1
        return sub
      })
      this.queryParam.completeEmployeeExternal = val
      if (this.queryParam.completeEmployeeInside) {
        this.queryParam.completeEmployee = this.queryParam.completeEmployeeInside.concat(val)
        this.$forceUpdate()
      } else {
        this.queryParam.completeEmployee = val
        this.$forceUpdate()
      }
    },
    disabledDate(current) {
      if (!this.declare.declareDate) return false
      else {
        let startDate
        if (typeof this.declare.declareDate === 'string') {
          startDate = moment(this.declare.declareDate)
        } else {
          startDate = this.declare.declareDate
        }
        return current && current < startDate
      }
    },
    async getIndexInfoList() {
      this.loading = true
      let res = await titleLabelQuery()
      this.loading = false
      if (res.code === this.SUCCESS_CODE) {
        this.processTreeData(res.data)
        this.indexInfoList = res.data
      }
    },
    processTreeData(list) {
      list.map(v => {
        if (v.children && v.children.length) {
          v.selectable = false
          this.processTreeData(v.children)
          return v
        }
      })
    },
    findTreeDataName(list, id) {
      let res
      for (let i = 0; i < list.length; i++) {
        if (res) break
        let v = list[i]
        if (v.id === id) {
          res = v.name
        } else {
          if (v.children && v.children.length) {
            res = this.findTreeDataName(v.children, id)
          }
        }
      }
      return res
    },
    async getData() {
      this.loading = true
      let res1 = await getClassifyList({ status: '1' })
      if (res1.code == this.SUCCESS_CODE) {
        this.ClassifyList = res1.data
      }
      let res = await getDeclare({ id: this.id })
      this.loading = false
      if (res.code === this.SUCCESS_CODE) {
        this.form = res.data
        this.declare = res.data.sciencesDeclare
        this.Plan = res.data.sciencesPlan
        this.queryParam = Object.assign(
          {
            completionStatus: undefined,
            completeEmployee: [],
            fileArrays: [],
            beginAndEndTime: '',
            indexInfo: [],
            completeOrgDTOS: [],
            completeEmployeeInside: [],
            completeEmployeeExternal: []
          },
          res.data.sciencesComplete
        )
        if (this.queryParam.indexInfo) {
          try {
            this.queryParam.indexInfo = JSON.parse(this.queryParam.indexInfo)
          } catch (e) {
            this.queryParam.indexInfo = []
          }
        } else {
          this.queryParam.indexInfo = []
        }
        if (this.queryParam.startAndEndTime) {
          this.queryParam.startAndEndTime = JSON.parse(this.queryParam.startAndEndTime)
        }

        if (this.queryParam.completeEmployee) {
          this.queryParam.completeEmployee.forEach(v => {
            if (v.type === 0) {
              this.queryParam.completeEmployeeInside.push({
                name: v.empName,
                id: v.empId,
                sortNum: v.sortNum,
                type: v.type
              })
            } else if (v.type == 1) {
              this.queryParam.completeEmployeeExternal.push({
                name: v.empName,
                id: v.empId,
                sortNum: v.sortNum,
                type: v.type
              })
            }
          })
          this.queryParam.completeEmployee = this.queryParam.completeEmployee.map((v, index) => {
            return { name: v.empName, id: v.empId, sortNum: index, type: v.type }
          })
        }
        if (this.queryParam.completeOrgDTOS) {
          this.queryParam.completeData = this.queryParam.completeOrgDTOS.map((v, index) => {
            return v.completeOrgId
          })
          this.completeListData = this.queryParam.completeOrgDTOS.map((v, index) => {
            return v.completeOrgName
          })
        }

        res.data.completeStatusNameCode && (this.queryParam.completionStatus = res.data.completeStatusNameCode.toString())
        this.queryParam.achievementsTypeId && (await this.getLevelList(this.queryParam.achievementsTypeId, true))
        this.queryParam.completeAchievementsLevelId && this.completeAchievementsLevelIdChange(this.queryParam.completeAchievementsLevelId, true)
      }
      setTimeout(() => {
        this.init = true
      }, 100)
    },
    async getLevelList(typeId, init = false) {
      // this.queryParam.completeEmployee=  []
      // this.queryParam.completeOrgName=  []
      this.$refs.form.clearValidate()
      if (!init) {
        this.$set(this.queryParam, 'completeAchievementsLevelId', '')
      }
      let temp = this.ClassifyList.find(v => v.id === typeId)
      if (temp) {
        this.achievementsTypeName = temp.name
      }
      let res = await getGradeList({ typeId: this.queryParam.achievementsTypeId, status: '1' })
      if (res.code === this.SUCCESS_CODE) {
        this.LevelList = res.data.data
      }
    },
    completeAchievementsLevelIdChange(e, init = false) {
      let temp = this.LevelList.find(v => v.id === e)
      if (temp) {
        this.completeAchievementsLevelName1 = temp.levelName
        if (!init) {
          this.queryParam.completeEmployee = []
        }
      }
    },
    submitComplete() {
      this.setProcessConfig = null
      this.$refs.form.validate(async valid => {
        if (valid) {
          if(!this.queryParam.completeData || this.queryParam.completeData.length<1){
            this.$noticeWarning('请填写成果完成单位')
            return
          }
          this.submitApprovalConfirm(this.processConfig, async flag => {
            if (!flag) {
              return
            }
            let temp = this.$clone(this.queryParam)
            temp.completeEmployee = this.queryParam.completeEmployee.map((v, index) => {
              return { empName: v.name, empId: v.id, sortNum: index, type: v.type }
            })
            if (temp.startAndEndTime) {
              temp.startAndEndTime = JSON.stringify(temp.startAndEndTime)
            }
            temp.strIndexInfo = []
            temp.indexInfo.map(v => {
              let res = this.findTreeDataName(this.indexInfoList, v)
              temp.strIndexInfo.push(res)
            })
            temp.strIndexInfo = temp.strIndexInfo.join(',')
            temp.indexInfo = JSON.stringify(temp.indexInfo)
            temp.achievementsId = this.id
            temp.supplement = 0
            this.loading = true
            delete temp.completeEmployeeExternal
            delete temp.completeEmployeeInside
            delete temp.completeOrgName

            let res = await addComplete(temp)
            this.loading = false
            if (res.code === this.SUCCESS_CODE) {
              this.$noticeSuccess('提交成功')
              //完成状态为4，才发起流程
              if (this.queryParam.completionStatus * 1 === 4) {
                this.businessId = res.data.id
                this.setProcessConfig = this.processConfig
              } else {
                this.$close()
              }
            }
          })
        } else {
          this.$noticeWarning('请填写完整')
        }
      })
    },
    onChange(value) {
      if (value.length > 3) {
        this.$noticeWarning('只能选中三条数据')
      } else {
        this.queryParam.indexInfo = value
      }
    }
  }
}
</script>
<style lang="less" scoped>
/deep/.active {
  background-color: #409eff;
}
</style>
